﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace EarthImitate
{
    public class Drawing
    {
        public static void DoiHeToaDo(ref Point3D p1, float R, double phi, double teta)
        {
            double x = p1.x, y = p1.y, z = p1.z;
            p1.x = Math.Round(-x * Math.Sin(teta) + y * Math.Cos(teta));
            p1.y = Math.Round(-x * Math.Cos(teta) * Math.Sin(phi) - y * Math.Sin(teta) * Math.Sin(phi) + z * Math.Cos(phi));
            p1.z = Math.Round(-x * Math.Cos(teta) * Math.Cos(phi) - y * Math.Sin(teta) * Math.Cos(phi) - z * Math.Sin(phi) + R);

        }
        public static void MyLine(Point2D p0, Color color, Point2D p1, Point2D p2, Graphics g)
        {
            p1.y = p0.y - p1.y;
            p2.y = p0.y - p2.y;
            p1.x = p1.x + p0.x;
            p2.x = p2.x + p0.x;
            g.DrawLine(new Pen(color), (float)p1.x, (float)p1.y, (float)p2.x, (float)p2.y);

        }
        /// <summary>
        /// Chieus the S soxy.
        /// </summary>
        /// <param name="p">The p.</param>
        /// <returns></returns>
        public static Point2D chieuSSoxy(Point3D p)
        {
            Point2D q = new Point2D();
            q.x = p.x;
            q.y = p.y;
            return q;
        }

        public static Point2D chieuPCoxy(Point3D p, int e)
        {
            Point2D q = new Point2D();
            q.x = p.x / (1 - p.z / e);
            q.y = p.y / (1 - p.z / e);
            return q;
        }
        /// <summary>
        /// Xoays the diem OX.
        /// </summary>
        /// <param name="p">The p.</param>
        /// <param name="a">A.</param>
        /// <returns></returns>
        public static Point3D xoayDiemOX(Point3D p, float a)
        {
            Point3D q = new Point3D();
            q.y = p.y * Math.Cos(a) - p.z * Math.Sin(a);
            q.z = p.y * Math.Sin(a) + p.z * Math.Cos(a);
            q.x = p.x;
            return q;
        }

        /// <summary>
        /// Xoays the diem OY.
        /// </summary>
        /// <param name="p">The p.</param>
        /// <param name="a">A.</param>
        /// <returns></returns>
        public static Point3D xoayDiemOY(Point3D p, float a)
        {
            Point3D q = new Point3D();

            q.z = p.z * Math.Cos(a) - p.x * Math.Sin(a);
            q.x = p.z * Math.Sin(a) + p.x * Math.Cos(a);
            q.y = p.y;

            return q;
        }

        /// <summary>
        /// Xoays the diem OZ.
        /// </summary>
        /// <param name="p">The p.</param>
        /// <param name="a">A.</param>
        /// <returns></returns>
        public static Point3D xoayDiemOZ(Point3D p, float a)
        {
            Point3D q = new Point3D();
            q.x = p.x * Math.Cos(a) - p.y * Math.Sin(a);
            q.y = p.x * Math.Sin(a) + p.y * Math.Cos(a);
            q.z = p.z;
            return q;
        }

        /// <summary>
        /// Xoays the wireframe OX.
        /// </summary>
        /// <param name="w">The w.</param>
        /// <param name="a">A.</param>
        public static void xoayWireframeOX(ref Wireframe w, float a)
        {
            for (int i = 0; i < w.NumVertex; i++)
                w.Vertexes[i] = xoayDiemOX(w.Vertexes[i], a);
        }
        /// <summary>
        /// Xoays the wireframe OY.
        /// </summary>
        /// <param name="w">The w.</param>
        /// <param name="a">A.</param>
        public static void xoayWireframeOY(ref Wireframe w, float a)
        {
            for (int i = 0; i < w.NumVertex; i++)
                w.Vertexes[i] = xoayDiemOY(w.Vertexes[i], a);
        }
        /// <summary>
        /// Xoays the wireframe OZ.
        /// </summary>
        /// <param name="w">The w.</param>
        /// <param name="a">A.</param>
        public static  void xoayWireframeOZ(ref Wireframe w, float a)
        {
            for (int i = 0; i < w.NumVertex; i++)
                w.Vertexes[i] = xoayDiemOZ(w.Vertexes[i], a);
        }
        public static void veSS_viewxyMT(Graphics graphics,B_Rep w, double phiMatTroi, double tetaMatTroi,Point2D p0, Color color)
        {
            int j;

            var p1 = new Point2D();
            var p2 = new Point2D();
           
            for (int i = 0; i < w.nPoly - 2; i++)
            {
                if (w.poly[i].Visible == 1)
                {
                    for (j = 0; j < w.poly[i].NVert - 1; j++)
                    {
                        Point3D p13D = w.vert[w.poly[i].Vertexes[j]];

                        DoiHeToaDo(ref p13D, 200, phiMatTroi, tetaMatTroi);

                        p1.x = p13D.x;
                        p1.y = p13D.y;
                        //p1 = this.chieuPCoxy(p13D, 500);

                        Point3D p23D = w.vert[w.poly[i].Vertexes[j + 1]];

                        DoiHeToaDo(ref p23D, 200, phiMatTroi, tetaMatTroi);

                        p2.x = p23D.x;
                        p2.y = p23D.y;

                        //p2 = this.chieuPCoxy(p23D, 500);

                        //MyLine(p1, p2, this.graphics);
                        MyLine(p0, color, p1, p2, graphics);
                    }

                    Point3D p113D = w.vert[w.poly[i].Vertexes[0]];

                    DoiHeToaDo(ref p113D, 200, phiMatTroi, tetaMatTroi);

                    p1.x = p113D.x;
                    p1.y = p113D.y;

                    Point3D p123D = w.vert[w.poly[i].Vertexes[j]];

                    DoiHeToaDo(ref p123D, 200, phiMatTroi, tetaMatTroi);

                    p2.x = p123D.x;
                    p2.y = p123D.y;

                    MyLine(p0, color, p1, p2, graphics);

                   
                }
            }
        }
        public static void tinhPhapVector(ref B_Rep w, double phiTraiDat, double tetaTraiDat)
        {
            Point3D p1 = new Point3D(); 
            Point3D p2 = new Point3D();
            Point3D p3 = new Point3D();
            Vector n = new Vector();

            for (int i = 0; i < w.nPoly - 2; i++)
            {

                p1 = w.vert[w.poly[i].Vertexes[0]];
                p2 = w.vert[w.poly[i].Vertexes[1]];
                p3 = w.vert[w.poly[i].Vertexes[2]];

                DoiHeToaDo(ref p1, 200, phiTraiDat, tetaTraiDat);
                DoiHeToaDo(ref p2, 200, phiTraiDat, tetaTraiDat);
                DoiHeToaDo(ref p3, 200, phiTraiDat, tetaTraiDat);

                n.x = p1.y * (p2.z - p3.z) + p2.y * (p3.z - p1.z) + p3.y * (p1.z - p2.z);
                n.y = p1.z * (p2.x - p3.x) + p2.z * (p3.x - p1.x) + p3.z * (p1.x - p2.x);
                n.z = p1.x * (p2.y - p3.y) + p2.x * (p3.y - p1.y) + p3.x * (p1.y - p2.y);
                w.poly[i].PVector = n;
            }
        }
        public static void bienB_repOX(ref B_Rep w, float a)
        {
            for (int i = 0; i < w.nVert; i++)
            {
                w.vert[i] = xoayDiemOX(w.vert[i], a);
            }
        }
        public static void bienB_repOY(ref B_Rep w, float a)
        {
            for (int i = 0; i < w.nVert; i++)
            {
                w.vert[i] = xoayDiemOY(w.vert[i], a);
            }
        }
        public static void bienB_repOZ(ref B_Rep w, float a)
        {
            for (int i = 0; i < w.nVert; i++)
            {
                w.vert[i] = xoayDiemOZ(w.vert[i], a);
            }
        }

        //--------------- Khu mat khuat ------------------

        public static void khuMatKhuat(ref B_Rep w, Vector v)
        {
            Vector n;
            for (int i = 0; i < w.nPoly - 2; i++)
            {
                n = w.poly[i].PVector;
                if (n.x * v.x + n.y * v.y + n.z * v.z < 0) w.poly[i].Visible = 0;
                else w.poly[i].Visible = 1;
            }
        }

    }
}
